package com.yunfei.api.v2.qiuzhi.service.impl;

import com.yunfei.api.v2.qiuzhi.contants.QiuZhiContants;
import com.yunfei.api.v2.qiuzhi.result.HuiDaResult;
import com.yunfei.api.v2.qiuzhi.result.QiuZhiResult;
import com.yunfei.api.v2.qiuzhi.service.QiuZhiHuiDaService;
import com.yunfei.api.v2.user.valid.HuiDaValid;
import com.yunfei.api.v2.user.valid.HuiFuValid;
import com.yunfei.api.v2.user.valid.TokenForm;
import com.yunfei.bo.SkAnswDtlBo;
import com.yunfei.bo.SkQuesDtlBo;
import com.yunfei.common.json.result.ResultModel;
import com.yunfei.core.utils.TokenUtil;
import com.yunfei.core.utils.enmu.QuesResultStatus;
import com.yunfei.core.utils.enmu.ResultStatus;
import com.yunfei.core.utils.id.IdUtils;
import com.yunfei.core.utils.string.StringUtil;
import com.yunfei.mapper.aiiidb.SkAnswerDtlMapper;
import com.yunfei.mapper.aiiidb.SkQuesDtlMapper;
import com.yunfei.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: zntg_v1
 * @description:
 * @author: zhengyunfei
 * @create: 2018-10-30 15:48
 **/
@Service("qiuZhiHuiDaService")
public class QiuZhiHuiDaServiceImpl implements QiuZhiHuiDaService {
    @Autowired
    private SkAnswerDtlMapper skAnswerDtlMapper;
    @Autowired
    private SkQuesDtlMapper skQuesDtlMapper;
    @Autowired
    private RedisService redisService;
    /**
     * @Description: 求知广场:回答
     * @Author: zhengyunfei
     * @Date: 2018/11/1
     * @time:10:36
     */
    public ResultModel huida(HuiDaValid requestParam, TokenForm tokenForm, ResultModel resultModel) {
        String answDtl=requestParam.getAnswDtl();
        String quesId=requestParam.getQuesId();
        String token=tokenForm.getToken();
        if(StringUtil.isNullOrEmpty(token)){
            resultModel.setCode(ResultStatus.TOKEN_NOTNULL.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_NOTNULL.getMessage());
            return resultModel;
        }
        if(token.length()<64){
            resultModel.setCode(ResultStatus.TOKEN_WUXIAO.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_WUXIAO.getMessage());
            return resultModel;
        }
        if(StringUtil.isNullOrEmpty(answDtl)){
            resultModel.setCode(QuesResultStatus.ANSW_DTL_NOTNULL.getCode());
            resultModel.setMessage(QuesResultStatus.ANSW_DTL_NOTNULL.getMessage());
            return resultModel;
        }
        if(redisService.exists(token)){
            SkAnswDtlBo skQuesDtlBo=new SkAnswDtlBo();
            String answId= IdUtils.getId();
            skQuesDtlBo.setQuesId(quesId);
            skQuesDtlBo.setAnswId(answId);
            skQuesDtlBo.setAnswDtl(answDtl);
            String userId= TokenUtil.getInstance().getUserId(token);
            skQuesDtlBo.setUserId(userId);
            String checkFlg=QiuZhiContants.CHECK_FLG_W;//审核标识待审核
            skQuesDtlBo.setAnswCheckFlg(checkFlg);
            skAnswerDtlMapper.add(skQuesDtlBo);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            return resultModel;

        }else{
            resultModel.setCode(ResultStatus.TOKEN_ERROR.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_ERROR.getMessage());
            return resultModel;
        }
    }
    public ResultModel moreHuida(HuiDaValid requestParam, TokenForm tokenForm, ResultModel resultModel) {
        Map<String,Object> result=new HashMap<>();
        String answDtl=requestParam.getAnswDtl();
        String quesId=requestParam.getQuesId();
        String token=tokenForm.getToken();
        if(StringUtil.isNullOrEmpty(token)){
            resultModel.setCode(ResultStatus.TOKEN_NOTNULL.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_NOTNULL.getMessage());
            return resultModel;
        }
        if(StringUtil.isNullOrEmpty(answDtl)){
            resultModel.setCode(QuesResultStatus.ANSW_DTL_NOTNULL.getCode());
            resultModel.setMessage(QuesResultStatus.ANSW_DTL_NOTNULL.getMessage());
            return resultModel;
        }
        Map<String,Object> questionMap=new HashMap<>();
        questionMap.put("quesId",quesId);
        if(redisService.exists(token)){
            String userId= TokenUtil.getInstance().getUserId(token);
            QiuZhiResult quesDtlBo= (QiuZhiResult) skQuesDtlMapper.getMoreHuidaQuestion(questionMap);
            result.put("question",quesDtlBo);
            Map<String,Object> huidaMap=new HashMap<>();
            huidaMap.put("quesId",quesId);
            huidaMap.put("userId",userId);
            List<HuiDaResult> answerList=skQuesDtlMapper.getMoreHuidaAnswers(huidaMap);
            result.put("answerList",answerList);
            resultModel.setData(result);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            return resultModel;
        }else{
            resultModel.setCode(ResultStatus.TOKEN_ERROR.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_ERROR.getMessage());
            return resultModel;
        }
    }

    /**
     * @Description: 求知广场:回复
     * @Author: zhengyunfei
     * @Date: 2018/11/1
     * @time:10:36
     */
    public ResultModel huifu(HuiFuValid requestParam, TokenForm tokenForm, ResultModel resultModel) {
        String answDtl=requestParam.getAnswDtl();
        String quesId=requestParam.getQuesId();
        String answQuesId=requestParam.getAnswQuesId();
        String answUserId=requestParam.getAnswUserId();
        String token=tokenForm.getToken();
        if(StringUtil.isNullOrEmpty(token)){
            resultModel.setCode(ResultStatus.TOKEN_NOTNULL.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_NOTNULL.getMessage());
            return resultModel;
        }
        if(token.length()<64){
            resultModel.setCode(ResultStatus.TOKEN_WUXIAO.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_WUXIAO.getMessage());
            return resultModel;
        }
        if(StringUtil.isNullOrEmpty(answDtl)){
            resultModel.setCode(QuesResultStatus.ANSW_DTL_NOTNULL.getCode());
            resultModel.setMessage(QuesResultStatus.ANSW_DTL_NOTNULL.getMessage());
            return resultModel;
        }
        if(redisService.exists(token)){
            SkAnswDtlBo skQuesDtlBo=new SkAnswDtlBo();
            String answId= IdUtils.getId();
            skQuesDtlBo.setQuesId(quesId);
            skQuesDtlBo.setAnswId(answId);
            skQuesDtlBo.setAnswDtl(answDtl);
            String userId= TokenUtil.getInstance().getUserId(token);
            skQuesDtlBo.setUserId(userId);
            skQuesDtlBo.setAnswQuesId(answQuesId);
            skQuesDtlBo.setAnswUserId(answUserId);
            String checkFlg=QiuZhiContants.CHECK_FLG_W;//审核标识待审核
            skQuesDtlBo.setAnswCheckFlg(checkFlg);
            skAnswerDtlMapper.add(skQuesDtlBo);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            return resultModel;

        }else{
            resultModel.setCode(ResultStatus.TOKEN_ERROR.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_ERROR.getMessage());
            return resultModel;
        }
    }



}
